<template>
	<view class="containar">
		<view class="avatarUrl">
			<button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
				<image :src="avatarUrl" class="refreshIcon"></image>
			</button>
		</view>
		<view class="nickname">
			<text>昵称：</text>
			<input type="nickname" class="weui-input" :value="nickName" @blur="bindblur" placeholder="请输入昵称"
				@input="bindinput" />
		</view>
 
		<view class="btn">
			<!-- <view class="btn-sub" @click="onSubmit">保存</view> -->
			<u-button type="primary" shape="circle" text="保存" @click="onSubmit"></u-button>
		</view>
	</view>
</template>
 
<script>
	const Auth = require("@/utils/auth.js");
	export default {
		data() {
			return {
				avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
				nickName: ''
			};
		},
		onLoad(option) {},
		methods: {
			bindblur(e) {
				// 获取微信昵称
				this.nickName = e.detail.value;
			},
			bindinput(e) {
				//这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮，会出现修改不成功的情况。
				this.nickName = e.detail.value;
			},
			onChooseavatar(e) {
				console.log(e)
				this.avatarUrl = e.detail.avatarUrl;
			},
			onSubmit() {
				if (this.nickName === '') {
					uni.showToast({
						icon: 'none',
						title: '请输入昵称'
					})
					return false;
				}
				uni.showLoading({
					title: '头像上传中...'
				});
				
				
				let obj = {}
				
				obj.token = Auth.getToken();
				
				// #ifdef MP-WEIXIN
				obj.os = 'wx';
				// #endif
				
				// #ifdef MP-BAIDU
				obj.os = 'bd';
				// #endif
				
				// #ifdef MP-QQ
				obj.os = 'qq';
				// #endif
				// uni.request({
				//     url: 'https://wshop.bolearn.cn/wp-json/zhuige-shop/setting/save_file', //仅为示例，并非真实接口地址。
				//     data: {
				// 		os: "wx",
				// 		token: Auth.getToken()
				//     },
				//     success: (res) => {
				//         console.log(res.data);
				//         this.text = 'request success';
				//     }
				// });
				
				uni.uploadFile({
					url: "https://wshop.bolearn.cn/wp-json/zhuige-shop/setting/save_file?os=wx&token="+Auth.getToken(), // 仅为示例，非真实的接口地址
					filePath: this.avatarUrl,
					name: 'file',
					header: {
						'content-type': 'multipart/form-data',
					},
					formData: {
						os: "wx",
						token: Auth.getToken()
					},
					success: (res) => {
						uni.showToast({
							title: '上传成功',
							icon: 'success'
						})
					},
					fail: (res) => {
						
					}
				});
				
			}
		}
	};
</script>
<style lang="scss">
	.containar {
		.avatarUrl {
			padding: 80rpx 0 40rpx;
			background: #fff;
 
			button {
				background: #fff;
				line-height: 80rpx;
				height: auto;
				width: auto;
				padding: 20rpx 30rpx;
				margin: 0;
				display: flex;
				justify-content: center;
				align-items: center;
 
 
				.refreshIcon {
					width: 160rpx;
					height: 160rpx;
					border-radius: 50%;
				}
 
				.jt {
					width: 14rpx;
					height: 28rpx;
				}
			}
		}
 
		// botton 去除边框
		button::after {
			border: none;
		}
 
		.nickname {
			background: #fff;
			padding: 20rpx 30rpx 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
 
			.weui-input {
				padding-left: 60rpx;
			}
		}
 
		.btn {
			width: 80%;
			margin: 0 auto;
 
			// .btn-sub {
			// 	width: 670rpx;
			// 	margin: 80rpx auto 0;
			// 	height: 90rpx;
			// 	background: #DF8585;
			// 	border-radius: 45rpx;
			// 	line-height: 90rpx;
			// 	text-align: center;
			// 	font-size: 36rpx;
			// 	color: #fff;
			// }
		}
	}
</style>